﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using WebCrawlerTools.web;

namespace WebCrawlerTools.search
{
    public class SRDomParse
    {
        internal static System.Windows.Forms.HtmlElement GetNextPageLink()
        {
            HtmlElementCollection link_elements = WebEngine.Web.Document.Links;
            foreach (HtmlElement link in link_elements)
            {
                if (link.InnerText == "Next")
                {
                    return link;
                }
            }
            return null;
        }

        internal static SResult GetPersonList()
        {
            //set title
            SResult Result = new SResult();
            Result.Title.Data.Add("Name");
            Result.Title.Data.Add("Title");
            Result.Title.Data.Add("Link");
            Result.Title.Data.Add("Img");

            //set data
            HtmlElementCollection div_elements = WebEngine.Web.Document.GetElementsByTagName("div");
            foreach(HtmlElement div in div_elements)
            {
                string style = div.GetAttribute("className");
                if (style !=null && style.StartsWith("x-grid3-row"))
                {
                    SLine person = GetPersonInfo(div);
                    Result.Data.Add(person);
                }
            }
            return Result;
        }

        private static SLine GetPersonInfo(HtmlElement person_element)
        {
            SLine person = new SLine();
            string name = "";
            string title = "";
            string link = "";
            string img = "";
            //get Name
            HtmlElementCollection div_elements = person_element.GetElementsByTagName("a");
            foreach (HtmlElement div in div_elements)
            {
                string style = div.GetAttribute("className");
                if (style != null && style.StartsWith("chatterlisticon"))
                {
                    link = div.GetAttribute("href");
                    break;
                }
            }

            //get link
            HtmlElementCollection link_elements = person_element.GetElementsByTagName("div");
            foreach (HtmlElement l in link_elements)
            {
                string style = l.GetAttribute("className");
                if (style != null && style.StartsWith("displayName"))
                {
                    name = l.InnerText;
                    HtmlElement t = l.NextSibling;
                    if (t != null)
                    {
                        title = t.InnerText;
                    }
                    break;
                }
            }
            //get img
            HtmlElementCollection img_elements = person_element.GetElementsByTagName("img");
            foreach (HtmlElement i in img_elements)
            {
                string src = i.GetAttribute("src");
                if (src != null)
                {
                    img = src;
                    break;
                }
            }
            person.Data.Add(name);
            person.Data.Add(title);
            person.Data.Add(link);
            person.Data.Add(img);
            return person;
        }
    }
}
